#!/usr/bin/env bash
#

if [ $# -ne 1 ]; then
	echo "Please give the path to the file with student records."
	exit 1
fi

student_list_file=$1

read -r num_rows num_cols <<< $(stty size)

bg_red=$(tput setab 1)
normal=$(tput sgr0)
fg_yellow8="$(tput setaf 3)";

# https://www.cyberciti.biz/faq/how-to-display-countdown-timer-in-bash-shell-script-running-on-linuxunix/
countdown()
(
  IFS=:
  set -- $*
  secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
  while [ $secs -gt 0 ]
  do
    sleep 1 &
    tput cup $(( $num_rows/2+1 )) $(( $num_cols/2-7 ))
    printf "${bg_red}%02d:%02d:%02d${normal}" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))
    secs=$(( $secs - 1 ))
    wait
  done
  echo
)

tput cup $(( $num_rows/2 )) $(( $num_cols/2-7 )) && echo "${fg_yellow8}$(shuf $student_list_file |head -n1)${normal}" && countdown "00:00:05" && mpg123 -q alarm_3beeps.mp3 
